
#include<stdlib.h>
#include<time.h>
//#include<gl/freeglut.h>
#include<gl/glut.h> 

#include"paintShape.h"
#include"types.h"


/* DECLARATIONS */
/*---------------------------------------------------------------------------------*/
void	reshape( int width, int height );
void	RenderCallBack(void);
void	prepShapes( int argc, char** argv );
void	destroyShapes();
/*---------------------------------------------------------------------------------*/

/* GLOBALS */
/*---------------------------------------------------------------------------------*/
Shape*	shapes;
int		many_shapes;
/*---------------------------------------------------------------------------------*/

/*  MAIN  */
/*---------------------------------------------------------------------------------*/
int main( int argc, char** argv )
{
	//preps
	inits();
	prepShapes( argc, argv );

	glutInitWindowPosition(20,20);

	// define position of window in the screen
	glutInitWindowSize(500,500);

	// define the window size
	glutInit( &argc, argv );

	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();

	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();

	// initialize OpenGL
	glutInitDisplayMode(GLUT_RGBA);

	// define display mode
	glutCreateWindow("hw1");
	glutDisplayFunc(RenderCallBack);

	// passes the drawing function reference
	glutReshapeFunc(reshape);
	glutMainLoop();

	destroyShapes();

	return 0;
}
/*---------------------------------------------------------------------------------*/

/* helpers */
/*---------------------------------------------------------------------------------*/
void prepShapes( int argc, char** argv )
{
	ShapeType t;
	int i, j, n=0, cur=0;
	many_shapes = 0;

	srand((unsigned)time(0));

	for( i=1 ; i<argc ; i+=2 )
	{
		n = atoi(argv[i]);
		cur = many_shapes;
		many_shapes += n;
		t = shapeTypeFromString(argv[i+1]);

		shapes = (Shape*) realloc( shapes, sizeof(Shape)*many_shapes );

		for( j=0 ; j<n ; ++j )
		{
			shapes[cur+j].color.r = (GLfloat) rand()/(float)RAND_MAX;
			shapes[cur+j].color.g = (GLfloat) rand()/(float)RAND_MAX;
			shapes[cur+j].color.b = (GLfloat) rand()/(float)RAND_MAX;

			shapes[cur+j].pos.x	= (GLfloat)(rand()%20 - 10);
			shapes[cur+j].pos.y	= (GLfloat)(rand()%20 - 10);
			shapes[cur+j].scale	= (GLfloat)(rand()%10);

			shapes[cur+j].width	= 1.0f;
			
			shapes[cur+j].shape_type = t;
		}
	}
}

void destroyShapes()
{ 
	int i;
	for( i=0 ; i<many_shapes ; ++i )
	{
		free( &shapes[i] );
	}
	free( shapes );
}
/*---------------------------------------------------------------------------------*/



/* glut plugs */
/*---------------------------------------------------------------------------------*/
void RenderCallBack(void)
{
	int i;

	glClearColor(0.5f,0.5f,0.5f,1.0f);
	glClear( GL_COLOR_BUFFER_BIT );
	
	for( i=0 ; i<many_shapes ; ++i )
	{
		paintShape( &shapes[i] );
	}
	glFlush();
}

void reshape( int width, int height )
{
	GLfloat aspect_ratio, left, right, top, bottom;

	left	=-20;
	right	= 20;
	top		= 20;
	bottom	=-20;

	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();

	glViewport( 0, 0, width, height );

	aspect_ratio = width / (GLfloat)height;

	if( aspect_ratio>=1 )
	{
		left	*= aspect_ratio;
		right	*= aspect_ratio;
	}

	else
	{
		top		/= aspect_ratio;
		bottom	/= aspect_ratio;
	}

	gluOrtho2D( left, right, bottom, top );
}
/*---------------------------------------------------------------------------------*/